//
//  HZMoreButtons.m
//  HZ_New_Stock
//
//  Created by gaotime on 16/6/15.
//  Copyright © 2016年 Zeroing. All rights reserved.
//

#import "HZMoreButtons.h"
#import "OSTools.h"
#import "ColorTools.h"
#import "AppInfo.h"
//#import "MineSelection.h"
//#import "ThemeManager.h"

#define TITLE_HEIGHT [OSTools AppSize].height * 0.0824
#define TITLE_LEFT [OSTools AppSize].width * 0.029
#define TITLE_RIGHT [OSTools AppSize].width * 0.048
#define TITLE_BOTTOM [OSTools AppSize].height * 0.0284
#define BOTTON_TAP [OSTools AppSize].height * 0.030

@interface HZMoreButtons()
@property (nonatomic, strong)UILabel *pTitle;
@property (nonatomic, strong)UIView *pLine;
@end

@implementation HZMoreButtons
@synthesize pParent;

-(id) initWithFrame:(CGRect) frame{
  fWidth = [OSTools AppSize].width;
  fHeight = [OSTools AppSize].height;
  if (self = [super initWithFrame:frame]) {
    _pTitle= [[UILabel alloc]initWithFrame:CGRectMake(TITLE_LEFT, 0, 100, TITLE_HEIGHT)];
    _pTitle.textColor = _rgb2uic(0x282828, 1);
    [self addSubview:_pTitle];
    
    float left = TITLE_LEFT+100+TITLE_RIGHT;
    _pLine =[[UIView alloc] initWithFrame:CGRectMake(left, TITLE_HEIGHT/2, fWidth-left, 0.5)];
    _pLine.backgroundColor=[UIColor lightGrayColor];
    [self addSubview:_pLine];
  }
  
  return self;
}

-(void)setData:(NSString *)title withButtons:(NSArray*)btnStrings{
//  int nThemeId = [[ThemeManager sharedThemeManager] themeIndex];
  // 设置Label的字体 HelveticaNeue  Courier
  UIFont *fnt = [UIFont systemFontOfSize:17];;
  _pTitle.font = fnt;
  // 根据字体得到NSString的尺寸
  CGSize size = [title sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:fnt,NSFontAttributeName, nil]];
  // 名字的H
//  CGFloat nameH = size.height;
  // 名字的W
  CGFloat nameW = size.width+title.length*5;
  _pTitle.frame = CGRectMake(TITLE_LEFT, 0, nameW,TITLE_HEIGHT);
  _pTitle.text = title;
  _pTitle.textColor = _rgb2uic(0x282828, 1);
  _pLine.backgroundColor=[UIColor lightGrayColor];
//  if (nThemeId == 1) {
//    _pTitle.textColor = _rgb2uic(0x57585c, 1);
//    _pLine.backgroundColor=_rgb2uic(0x3b4050,1);
//  }
  
  float left = TITLE_LEFT+nameW+TITLE_RIGHT;
  _pLine.frame = CGRectMake(left, TITLE_HEIGHT/2, fWidth-left-TITLE_RIGHT, 0.5);
  
  float fBiW = 1;
  if (fWidth == 320 ) {//320屏幕取的也是两倍，只能按比例降宽
    fBiW = 320.0/375.0;
  }else if(fWidth == 414){
    fBiW = 414.0/375.0;
  }
  
  float fBiH = 1;
  if (fHeight == 480 ) {//480屏幕取的也是两倍，只能按比例降宽
    fBiH = 480.0/667.0;
  }else if(fHeight == 568){
    fBiH = 568.0/667.0;
  }
  
  float itemW = fWidth/3;
  int row = (int)btnStrings.count/3;
  if (btnStrings.count%3 != 0) {
    row += 1;
  }

//  NSString *path = [ThemeManager sharedThemeManager].currentThemeHeader;
//  UIImage *image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@%@",path,@"/more_back"]];
  UIImage *image = [UIImage imageNamed:@"more_back"];
  
  float width = image.size.width*fBiW;
  float height= image.size.height*fBiH;
  float tap = (itemW-width)/2;
  for (int i = 0; i < row; i++) {
    for (int j = 0; j < 3; j++) {
      if (i*3+j == btnStrings.count) {
        return;
      }
      UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
//      button.tag = i;
      [button addTarget:self action:@selector(buttonClickAtIndex:) forControlEvents:UIControlEventTouchUpInside];
      button.frame = CGRectMake(tap+itemW*j, TITLE_HEIGHT+i*(height+BOTTON_TAP), width, height);
      [self addSubview:button];
      NSString *a_button_text = btnStrings[i*3+j];
      [button setTitle:a_button_text forState:UIControlStateNormal];
      [button setTitleColor:_rgb2uic(0x282828, 1) forState:UIControlStateNormal];
//      [button setBackgroundColor:_rgb2uic(0xf9f7f8, 1)];
//      if (nThemeId == 1) {
//        [button setTitleColor:_rgb2uic(0x57585c, 1) forState:UIControlStateNormal];
////        [button setBackgroundColor:_rgb2uic(0x1e1e28, 1)];
//      }
      button.titleLabel.font = fnt;
      [button setBackgroundImage:image forState:UIControlStateNormal];
    }
  }
}

- (void)buttonClickAtIndex:(id)sender {
    UIButton *button = (UIButton*)sender;
    [self.parent upToParentWithObjc:self withType:kHZ_stockListMore_Action withParamter:button.titleLabel.text];
//  [pParent OnAction:self withType:ACTIVE_MARKMORE_CLICKED withArg:button.titleLabel.text];
}

#pragma mark -
- (void)upToParentWithObjc:(id)objc withType:(NSInteger)type withParamter:(id)paramter{
}
- (void)downToChildWithObjc:(id)objc withType:(NSInteger)type withParamter:(id)paramter{
    switch (type) {
        case kHZ_updateNewData_Action:
        {
            if ([paramter isKindOfClass:[NSArray class]]) {
//                [self p_updateNewData:paramter];
            }
        }
            break;
            
        default:
            break;
    }
}
@end
